<?php

namespace data\Menu;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/4/16
 * Time: 9:17
 * 微信自定义菜单
 */
Class Menu{
    /**
     * 获取ACCESS_TOKEN
     */
    private function getAccessToken($appId,$appSecret){
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
        $data = self::getCurl($url);
        $resultArr = json_decode($data, true);
        return $resultArr["access_token"];
    }

    /**
     * get https的内容
     * @param $url
     * @return mixed
     */
    public function getCurl($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result =  curl_exec($ch);
        curl_close ($ch);
        return $result;
    }
    /**
     * 创建菜单
     * @param $menu:菜单json数据
     * @param $appId
     * @param $appSecret
     */
    public function CreateMenu($menu,$appId,$appSecret){
        $Access_Token = self::getAccessToken($appId,$appSecret);
        $mak_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$Access_Token;
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$mak_menu_url);
        curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"POST");
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch,CURLOPT_AUTOREFERER,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$menu);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        $info = curl_exec($ch);
        if(curl_errno($ch)) echo "Error".curl_error($ch);
        curl_close($ch);
        print_r($info);
    }
    public function getMenu($appId,$appSecret){
        $accessToken = self::getAccessToken($appId,$appSecret);
        $url = 'https://api.weixin.qq.com/cgi-bin/menu/get?access_token='.$accessToken;
        $result = file_get_contents($url);
        return json_encode(json_decode($result), JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
        //return self::jsonFormat(json_decode($result));
    }
    public static function jsonFormat($data, $indent=null){

    // 对数组中每个元素递归进行urlencode操作，保护中文字符
    array_walk_recursive($data, 'jsonFormatProtect');

    // json encode
    $data = json_encode($data);

    // 将urlencode的内容进行urldecode
    $data = urldecode($data);

    // 缩进处理
    $ret = '';
    $pos = 0;
    $length = strlen($data);
    $indent = isset($indent)? $indent : '    ';
    $newline = "\n";
    $prevchar = '';
    $outofquotes = true;

    for($i=0; $i<=$length; $i++){

        $char = substr($data, $i, 1);

        if($char=='"' && $prevchar!='\\'){
            $outofquotes = !$outofquotes;
        }elseif(($char=='}' || $char==']') && $outofquotes){
            $ret .= $newline;
            $pos --;
            for($j=0; $j<$pos; $j++){
                $ret .= $indent;
            }
        }

        $ret .= $char;

        if(($char==',' || $char=='{' || $char=='[') && $outofquotes){
            $ret .= $newline;
            if($char=='{' || $char=='['){
                $pos ++;
            }

            for($j=0; $j<$pos; $j++){
                $ret .= $indent;
            }
        }

        $prevchar = $char;
    }

    return $ret;
}

}